package com.hotel.comm.bean;

import com.google.gson.GsonBuilder;

import java.io.Serializable;
import java.util.List;

/**
 * 分页实体类
 *
 * @param <T>
 * @author XuSemon
 * @date 2015-4-14
 */
public class PageConfig<T> implements Serializable {
    /**
     *
     */
    private static final long serialVersionUID = 3498134718341L;
    private static final int DEFAULT_PAGE_SIZE = 10;
    /**
     * 当前页
     */
    private int currentPage = 1;
    /**
     * 每页记录数
     */
    private int pageSize = DEFAULT_PAGE_SIZE;
    /**
     * 记录总数
     */
    private int totalSize = 0;
    /**
     * 当前页记录集
     */
    private List<T> items;
    /**
     * 总页数
     */
    private int totalPageNum;

    /**
     * 默认构造函数
     */
    public PageConfig() {
    }

    /**
     * 构造函数
     *
     * @param currentPage
     * @param items
     * @param totalSize
     */
    public PageConfig(int currentPage, List<T> items, int totalSize) {
        this(DEFAULT_PAGE_SIZE, currentPage, items, totalSize);
    }

    /**
     * 构造函数
     *
     * @param pageSize
     * @param currentPage
     * @param items
     * @param totalSize
     */
    public PageConfig(int pageSize, int currentPage, List<T> items, int totalSize) {
        this.pageSize = pageSize;
        this.currentPage = currentPage;
        this.items = items;
        this.totalSize = totalSize;
        this.totalPageNum = this.getTotalPages();
    }

    /**
     * 获取总的页数,最多显示100页
     *
     * @return
     */
    public int getTotalPages() {
        int pageNum = (int) this.totalSize / this.pageSize;
        if (this.totalSize % this.pageSize > 0)
            return (pageNum + 1) > 100 ? 100 : pageNum + 1;
        else
            return pageNum > 100 ? 100 : pageNum;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalSize() {
        return totalSize;
    }

    public void setTotalSize(int totalSize) {
        this.totalSize = totalSize;
    }

    public List<T> getItems() {
        return items;
    }

    public void setItems(List<T> items) {
        this.items = items;
    }

    public int getTotalPageNum() {
        return totalPageNum;
    }

    public void setTotalPageNum(int totalPageNum) {
        this.totalPageNum = totalPageNum;
    }

    @Override
    public String toString() {
        return new GsonBuilder().create().toJson(this);
    }
}
